package hhh.app.presenter.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import hhh.app.R;
import hhh.app.common.Code;
import hhh.app.data.bean.Product;

/**
 * Created by Administrator on 2016/12/20 0020.
 */
public class SampleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_ITEM=0;
    private static final int TYPE_FOOTER=1;

    private List<Product> list;
    private Context context;

    public FootVH getFootVH() {
        return footVH;
    }

    private FootVH footVH;

    public SampleAdapter(Context context,List<Product> list) {
        this.context=context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==TYPE_FOOTER){
//            View view= LayoutInflater.from(context).inflate(R.layout.layout_list_footer,null);
//            StateLayout stateLayout=new StateLayout(context,R.layout.layout_list_footer);
//            return new FootVH(stateLayout);
            footVH=new FootVH(LayoutInflater.from(context).inflate(R.layout.view_loadmore,parent,false));
            return footVH;
        }else{
            View view= LayoutInflater.from(context).inflate(R.layout.layout_list_item,parent,false);
            return new ItemVH(context,view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ItemVH) {
            Product p=list.get(position);
            ((ItemVH) holder).setLabel(p.getName())
            .setRating((float) p.getRating())
            .setSpecial((float) p.getSpecial())
            .setPrice((float) p.getPrice())
            .setCount(20)
            .setIv(Code.baseImageUrl+p.getImage());
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position+1 == getItemCount()){
            return TYPE_FOOTER;
        }else{
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        return list.size()+1;
    }

    public class FootVH extends RecyclerView.ViewHolder{
        TextView footLabel;
        ProgressBar progressBar;
        public FootVH(View itemView) {
            super(itemView);
            footLabel=(TextView)itemView.findViewById(R.id.loadmore_txt);
            progressBar=(ProgressBar)itemView.findViewById(R.id.loadmore_progress);
        }

        public void setFootLabel(String s){
            footLabel.setText(s);
        }

        public void showProgress(boolean b){
            progressBar.setVisibility(b?View.VISIBLE:View.GONE);
        }
    }

//    public class FootVH extends RecyclerView.ViewHolder{
////        @BindView(R.id.loading_viewstub) View loading;
////        @BindView(R.id.end_viewstub) View end;
////        @BindView(R.id.network_error_viewstub) View error;
//        private View loading;
//        private View end;
//        private View error;
//
//        private StateLayout stateLayout;
//
//        public FootVH(View itemView) {
//            super(itemView);
////            ButterKnife.bind(this,itemView);
//            loading=itemView.findViewById(R.id.loading_viewstub);
//            end=itemView.findViewById(R.id.end_viewstub);
//            error=itemView.findViewById(R.id.network_error_viewstub);
//
//            if(itemView instanceof StateLayout){
//                stateLayout=(StateLayout)itemView;
//                showLoading();
//            }
//        }
//
//        public void showLoading(){
//            if(stateLayout!=null){
//                stateLayout.showView(R.id.loading_viewstub);
//            }
//        }
//
//        public void showError(){
//            if(stateLayout!=null){
//                stateLayout.showView(R.id.network_error_viewstub);
//            }
//        }
//
//        public void showEnd(){
//            if(stateLayout!=null){
//                stateLayout.showView(R.id.end_viewstub);
//            }
//        }
//    }

    class ItemVH extends RecyclerView.ViewHolder{
        Context context;
        @BindView(R.id.item_iv) ImageView iv;
        @BindView(R.id.item_label) TextView label;
        @BindView(R.id.item_rating) RatingBar rating;
        @BindView(R.id.item_special) TextView special;
        @BindView(R.id.item_price) TextView price;
        @BindView(R.id.item_sale_count) TextView count;

        public ItemVH(Context context,View itemView) {
            super(itemView);
            this.context=context;
            ButterKnife.bind(this,itemView);
        }

        public ItemVH setIv(String url){
            Picasso.with(context)
                    .load(url)
                    .into(iv);
            return this;
        }

        public ItemVH setLabel(String str){
            label.setText(str);
            return this;
        }

        public ItemVH setRating(float r){
            rating.setRating(r);
            return this;
        }

        public ItemVH setSpecial(float f){
            special.setText(String.valueOf(f));
            return this;
        }

        public ItemVH setPrice(float f){
            price.setText(String.valueOf(f));
            return this;
        }

        public ItemVH setCount(int c){
            count.setText("已售:"+c);
            return this;
        }
    }
}
